package com.gisocn.onemap.presenter;

import android.util.Log;
import com.gisocn.onemap.base.BasePresenter;
import com.gisocn.onemap.base.Constant;
import com.gisocn.onemap.interfaces.DownLoadListener;
import com.gisocn.onemap.utils.ExecutorsManager;
import com.gisocn.onemap.view.FileBrowseView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/* loaded from: classes2.dex */
public class FileBrowsePresent extends BasePresenter<FileBrowseView> {
    HttpURLConnection conn;
    private URL downLoadUrl = null;

    public FileBrowsePresent(FileBrowseView fileBrowseView) {
        attachView(fileBrowseView);
    }

    public void downLoadFile(final String str, final String str2, final String str3, final DownLoadListener downLoadListener) {
        ExecutorsManager.getInstance().execute(new Runnable() { // from class: com.gisocn.onemap.presenter.FileBrowsePresent.1
            @Override // java.lang.Runnable
            public void run() {
                try {
                    FileBrowsePresent.this.downLoadUrl = new URL(str);
                    try {
                        FileBrowsePresent fileBrowsePresent = FileBrowsePresent.this;
                        fileBrowsePresent.conn = (HttpURLConnection) fileBrowsePresent.downLoadUrl.openConnection();
                        long j = 0;
                        FileOutputStream fileOutputStream = null;
                        InputStream inputStream = FileBrowsePresent.this.conn.getInputStream();
                        long contentLength = FileBrowsePresent.this.conn.getContentLength();
                        try {
                            try {
                                File file = new File(Constant.SAVE_FILE_PACKAGE + str2 + str3);
                                Log.e("SAVE_FILE_PACKAGE", "" + Constant.SAVE_FILE_PACKAGE + str2);
                                downLoadListener.onStart();
                                FileOutputStream fileOutputStream2 = new FileOutputStream(FileBrowsePresent.this.getLocalFile(str, str2, str3));
                                try {
                                    byte[] bArr = new byte[1024];
                                    while (true) {
                                        int read = inputStream.read(bArr);
                                        if (read != -1) {
                                            fileOutputStream2.write(bArr, 0, read);
                                            j += read;
                                            long j2 = 100 * j;
                                            downLoadListener.onProgress((int) (j2 / contentLength));
                                            if (((int) (j2 / contentLength)) == 100) {
                                                downLoadListener.onFinish(file.getAbsolutePath());
                                                Log.e("savaFileName", "" + file.getAbsolutePath());
                                                ((FileBrowseView) FileBrowsePresent.this.mvpView).downLoadSuccess();
                                            }
                                        } else {
                                            try {
                                                break;
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
                                        }
                                    }
                                    fileOutputStream2.close();
                                    if (inputStream != null) {
                                        try {
                                            inputStream.close();
                                        } catch (IOException e2) {
                                            e = e2;
                                            e.printStackTrace();
                                        }
                                    }
                                } catch (FileNotFoundException e3) {
                                    e = e3;
                                    fileOutputStream = fileOutputStream2;
                                    e.printStackTrace();
                                    if (fileOutputStream != null) {
                                        try {
                                            fileOutputStream.close();
                                        } catch (IOException e4) {
                                            e4.printStackTrace();
                                        }
                                    }
                                    if (inputStream != null) {
                                        try {
                                            inputStream.close();
                                        } catch (IOException e5) {
                                            e = e5;
                                            e.printStackTrace();
                                        }
                                    }
                                } catch (IOException e6) {
                                    e = e6;
                                    fileOutputStream = fileOutputStream2;
                                    e.printStackTrace();
                                    if (fileOutputStream != null) {
                                        try {
                                            fileOutputStream.close();
                                        } catch (IOException e7) {
                                            e7.printStackTrace();
                                        }
                                    }
                                    if (inputStream != null) {
                                        try {
                                            inputStream.close();
                                        } catch (IOException e8) {
                                            e = e8;
                                            e.printStackTrace();
                                        }
                                    }
                                } catch (Throwable th) {
                                    th = th;
                                    fileOutputStream = fileOutputStream2;
                                    if (fileOutputStream != null) {
                                        try {
                                            fileOutputStream.close();
                                        } catch (IOException e9) {
                                            e9.printStackTrace();
                                        }
                                    }
                                    if (inputStream != null) {
                                        try {
                                            inputStream.close();
                                        } catch (IOException e10) {
                                            e10.printStackTrace();
                                        }
                                    }
                                    throw th;
                                }
                            } catch (FileNotFoundException e11) {
                                e = e11;
                            } catch (IOException e12) {
                                e = e12;
                            }
                        } catch (Throwable th2) {
                            th = th2;
                        }
                    } catch (IOException e13) {
                        e13.printStackTrace();
                    }
                } catch (MalformedURLException e14) {
                    e14.printStackTrace();
                }
            }
        });
    }

    /* JADX INFO: Access modifiers changed from: private */
    public File getLocalFile(String str, String str2, String str3) {
        if (str.equals("local")) {
            return new File(str2, str3);
        }
        return new File(Constant.SAVE_FILE_PACKAGE + str2, str3);
    }
}
